package mz.matafe.bolao

import mz.matafe.bolao.exception.*

/**
 * Bet Service.
 *
 * <p>Utility services for Bet
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class BetService{

    boolean transactional = true

    def matchService

    Bet doBet(User user, Bet bet) {
        def theUser = User.get(user.id)
        def match = bet.match;
        boolean isAvailable = matchService.isAvailableToBet(match)
        if (!isAvailable){
            throw new ServiceException(messageKey: 'Só pode apostar ate 1 hora antes do jogo!')
        }
        theUser.addToBets(bet)
        if (theUser.save(flush: true)) {
            return bet
        }
        else {
           throw new ServiceException(messageKey: 'Erros ao fazer a aposta. Aposta nao feita!')
        }
    }

}
